/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package assisisw.smartcal.webuisubsystem.managedbean;

import assisisw.smartcal.entitysubsystem.IUserController;
import assisisw.smartcal.entitysubsystem.User;
import assisisw.smartcal.entitysubsystem.exceptions.user.UserAlreadyRegisteredException;
import assisisw.smartcal.securitysubsystem.IMailVerifier;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;

/**
 *
 * @author Francesco
 */
@ManagedBean ( name = "signUpBean" , eager = true )
@Named ( "signUpBean" )
@ViewScoped
public class SignUpBean extends AbstractUserAnagraphicsViewBean 
{
  
    // Instance Fields
 
    /***/
    @EJB ()
    private IUserController userController ;
   
    @EJB ()
    private IMailVerifier mailVerifier ;
    
    // Instance Methods
    
    /***/
    public SignUpBean () 
    {
        super () ; 
    }
     
    public void initialize () 
    {
        setUserSigningUp ( new User () ) ; 
    }
    
    /**
     * AS THE SUPER'S ONE.
     */
    @Override
    public String submitUserAnagraphicForm () 
    {
        String token = "" ;
        String res ;
        try 
        {     
            token = mailVerifier.sendToken ( getUserSigningUp().getMail() ) ; // uncomment in production
            userController.registerUser ( getUserSigningUp().getMail() , 
                    getUserSigningUp().getPassword ()  , getUserSigningUp().getFirstname() , 
                    getUserSigningUp().getLastname() , token ) ;
            res = "registrationcompleted" ;
        } 
        catch ( UserAlreadyRegisteredException ex ) 
        {
            res = "unknownerror" ;
        }
        catch ( Exception ex ) 
        {
            res = "unknownerror" ;
        }
       
        return res ;
    }
       
}
